package com.um.jdy.common.device.utils;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.NumberUtil;
import com.um.springboot.starter.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @Title 绘图工具类
 * @Description
 * @Author renjunwei
 * @Date 2022/3/26 5:19 下午
 */
@Slf4j
@Component
public class DrawUtil {

    /**
     * 绘画二维码编号
     * @param background
     * @param no
     * @param targetPath
     * @return
     */
    public static String drawQrcodeNo(BufferedImage background, String no, String targetPath){

        //画布宽
        int width = 300;
        //画布高
        int height = 320;

        Font font = null;
        try {
            font = Font.createFont(Font.TRUETYPE_FONT,new ClassPathResource("PingFang-SC-Medium.ttf").getStream());
        } catch (Exception e) {
            log.error("添加PingFang-SC-Medium字体失败，原因：{}", e.toString());
        }

        //初始化画布
        BufferedImage output = new BufferedImage(getScale(width), getScale(height), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) output.getGraphics();

        //设置抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //填充白色背景色
        g2d.setColor(new Color(0xFFFFFFFF, true));
        g2d.fillRect(0, 0, getScale(width), getScale(height));

        try {
            g2d.drawImage(background.getScaledInstance(getScale(300), getScale(300), Image.SCALE_DEFAULT), 0, 0, null);
        } catch (Exception e) {
            log.error("绘制背景图失败，原因：{}", e.toString());
        }

        //绘制用户昵称
        g2d.setColor(new Color(0x222222));
        g2d.setFont(font.deriveFont(Font.PLAIN, getScale(24)));
        int contentLen = getContentLen(no, g2d);
        g2d.drawString(no, (getScale(width)-contentLen)/2, getScale(310));

        //绘制结束
        g2d.dispose();

        //输出
        try {
            FileUtil.mkdirs(targetPath);
            File file = new File(targetPath);

            ImageIO.write(output, "png", file);

            return targetPath;
        } catch (Exception e) {
            log.error("绘制图片输出classpath失败，原因：{}", e.toString());
        }

        return "";
    }

    /**
     * 缩放，解决按照原尺寸绘制模糊的问题
     * @param i
     * @return
     */
    private static int getScale(Number i){
        return NumberUtil.mul(i, 3).intValue();
    }

    /**
     * 获取文字长度
     * @param c
     * @param g2d
     * @return
     */
    public static int getCharLen(char c, Graphics2D g2d) {
        int length = g2d.getFontMetrics(g2d.getFont()).charWidth(c);
        return length;
    }

    /**
     * 获取文字总长度
     * @param content
     * @param g2d
     * @return
     */
    public static int getContentLen(String content, Graphics2D g2d) {
        int length = g2d.getFontMetrics(g2d.getFont()).charsWidth(content.toCharArray(), 0, content.length());
        return length;
    }

    /**
     * 绘制圆角图片
     * @param image
     * @return
     */
    public static BufferedImage toCircle(BufferedImage image) {
        int w = image.getWidth();
        int h = image.getHeight();
        BufferedImage output = new BufferedImage(w, h,
                BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2 = output.createGraphics();

        // This is what we want, but it only does hard-clipping, i.e. aliasing
        // g2.setClip(new RoundRectangle2D ...)

        // so instead fake soft-clipping by first drawing the desired clip shape
        // in fully opaque white with antialiasing enabled...
        g2.setComposite(AlphaComposite.Src);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.WHITE);
        g2.fill(new Ellipse2D.Double(0,0,w,h));

        // ... then compositing the image on top,
        // using the white shape from above as alpha source
        g2.setComposite(AlphaComposite.SrcAtop);
        g2.drawImage(image, 0, 0, null);

        g2.dispose();

        return output;
    }

}
